//
//  LuckyDayViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "LuckyDayViewController.h"
#import <Masonry/Masonry.h>

@interface LuckyDayViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation LuckyDayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.941 green:0.949 blue:0.961 alpha:1.0];
    
    // 头部导航
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    [self.view addSubview:self.headerView];
    
    // 返回按钮
     self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setTitle:@"←" forState:UIControlStateNormal];
    [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.backButton];
    
    // 标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel = @"择吉日";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:self.titleLabel];
    
    // 滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    // 内容视图
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    
    // 创建内容
    [self createContent];
    
    [self setupConstraints];
}

- (void)createContent {
    // 事件选择卡片
    UIView *eventCard = [[UIView alloc] init];
    eventCard.backgroundColor = [UIColor whiteColor];
    eventCard.layer.cornerRadius = 12;
    eventCard.layer.shadowColor = [UIColor blackColor].CGColor;
    eventCard.layer.shadowOffset = CGSizeMake(0, 2);
    eventCard.layer.shadowOpacity = 0.1;
    eventCard.layer.shadowRadius = 4;
    [self.contentView addSubview:eventCard];
    
    UILabel *eventTitle = [[UILabel alloc] init];
    eventTitle.text = @"选择事项";
    eventTitle.font = [UIFont boldSystemFontOfSize:18];
    eventTitle.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    [eventCard addSubview:eventTitle];
    
    // 事件网格
    UIView *eventGrid = [[UIView alloc] init];
    [eventCard addSubview:eventGrid];
    
    NSArray *events = @[
        @{@"name": @"婚嫁", @"icon": @"💕", @"selected": @YES},
        @{@"name": @"乔迁", @"icon": @"🏠", @"selected": @NO},
        @{@"name": @"开业", @"icon": @"🏪", @"selected": @NO},
        @{@"name": @"出行", @"icon": @"🚗", @"selected": @NO},
        @{@"name": @"入学", @"icon": @"🎓", @"selected": @NO},
        @{@"name": @"求职", @"icon": @"💼", @"selected": @NO},
        @{@"name": @"栽种", @"icon": @"🌱", @"selected": @NO},
        @{@"name": @"更多", @"icon": @"⋯", @"selected": @NO}
    ];
    
    for (int i = 0; i < events.count; i++) {
        NSDictionary *event = events[i];
        BOOL isSelected = [event[@"selected"] boolValue];
        
        UIView *eventView = [[UIView alloc] init];
        eventView.backgroundColor = isSelected ? [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:0.2] : [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
        eventView.layer.cornerRadius = 8;
        [eventGrid addSubview:eventView];
        
        UILabel *iconLabel = [[UILabel alloc] init];
        iconLabel.text = event[@"icon"];
        iconLabel.font = [UIFont systemFontOfSize:20];
        iconLabel.textAlignment = NSTextAlignmentCenter;
        iconLabel.textColor = isSelected ? [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0] : [UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0];
        [eventView addSubview:iconLabel];
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = event[@"name"];
        nameLabel.font = [UIFont systemFontOfSize:12];
        nameLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [eventView addSubview:nameLabel];
        
        // 添加点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eventTapped:)];
        eventView.tag = i;
        eventView.userInteractionEnabled = YES;
        [eventView addGestureRecognizer:tap];
        
        [eventView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(80);
            make.left.equalTo(eventGrid).offset((i % 4) * 88);
            make.top.equalTo(eventGrid).offset((i / 4) * 88);
        }];
        
        [iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(eventView);
            make.top.equalTo(eventView).offset(12);
        }];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(eventView);
            make.top.equalTo(iconLabel.mas_bottom).offset(4);
        }];
    }
    
    [eventTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(eventCard).offset(16);
    }];
    
    [eventGrid mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(eventTitle.mas_bottom).offset(16);
        make.left.right.bottom.equalTo(eventCard).inset(16);
        make.height.mas_equalTo(176);
    }];
    
    // 查询按钮
    UIButton *queryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [queryButton setTitle:@"查询吉日" forState:UIControlStateNormal];
    [queryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queryButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    queryButton.backgroundColor = [UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0];
    queryButton.layer.cornerRadius = 12;
    [queryButton addTarget:self action:@selector(queryButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:queryButton];
    
    // 查询结果卡片
    UIView *resultCard = [[UIView alloc] init];
    resultCard.backgroundColor = [UIColor whiteColor];
    resultCard.layer.cornerRadius = 12;
    resultCard.layer.shadowColor = [UIColor blackColor].CGColor;
    resultCard.layer.shadowOffset = CGSizeMake(0, 2);
    resultCard.layer.shadowOpacity = 0.1;
    resultCard.layer.shadowRadius = 4;
    [self.contentView addSubview:resultCard];
    
    UILabel *resultTitle = [[UILabel alloc] init];
    resultTitle.text = @"查询结果";
    resultTitle.font = [UIFont boldSystemFontOfSize:18];
    resultTitle.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    [resultCard addSubview:resultTitle];
    
    // 结果项
    NSArray *results = @[
        @{@"date": @"2023年10月18日", @"lunar": @"农历九月初四 星期三", @"level": @"上等日", @"conflict": @"冲鸡(乙酉)煞西"},
        @{@"date": @"2023年10月25日", @"lunar": @"农历九月十一 星期三", @"level": @"中等日", @"conflict": @"冲虎(戊寅)煞南"},
        @{@"date": @"2023年11月1日", @"lunar": @"农历九月十八 星期三", @"level": @"上等日", @"conflict": @"冲猴(甲申)煞北"}
    ];
    
    for (int i = 0; i < results.count; i++) {
        NSDictionary *result = results[i];
        
        UIView *resultItem = [[UIView alloc] init];
        resultItem.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.0];
        resultItem.layer.cornerRadius = 8;
        resultItem.layer.borderWidth = 1;
        resultItem.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0].CGColor;
        [resultCard addSubview:resultItem];
        
        UILabel *dateLabel = [[UILabel alloc] init];
        dateLabel.text = result[@"date"];
        dateLabel.font = [UIFont boldSystemFontOfSize:16];
        dateLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
        [resultItem addSubview:dateLabel];
        
        UILabel *lunarLabel = [[UILabel alloc] init];
        lunarLabel.text = result[@"lunar"];
        lunarLabel.font = [UIFont systemFontOfSize:12];
        lunarLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
        [resultItem addSubview:lunarLabel];
        
        UILabel *levelLabel = [[UILabel alloc] init];
        levelLabel.text = result[@"level"];
        levelLabel.font = [UIFont systemFontOfSize:12];
        levelLabel.textColor = [UIColor whiteColor];
        levelLabel.backgroundColor = [result[@"level"] isEqualToString:@"上等日"] ? [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0] : [UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0];
        levelLabel.layer.cornerRadius = 4;
        levelLabel.textAlignment = NSTextAlignmentCenter;
        [resultItem addSubview:levelLabel];
        
        UILabel *conflictLabel = [[UILabel alloc] init];
        conflictLabel.text = result[@"conflict"];
        conflictLabel.font = [UIFont systemFontOfSize:12];
        conflictLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
        [resultItem addSubview:conflictLabel];
        
        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [detailButton setTitle:@"查看详情 >" forState:UIControlStateNormal];
        [detailButton setTitleColor:[UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0] forState:UIControlStateNormal];
        detailButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [resultItem addSubview:detailButton];
        
        [resultItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(resultCard).inset(16);
            make.top.equalTo(resultCard).offset(48 + i * 80);
            make.height.mas_equalTo(72);
        }];
        
        [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(resultItem).offset(12);
        }];
        
        [lunarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(dateLabel);
            make.top.equalTo(dateLabel.mas_bottom).offset(4);
        }];
        
        [levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(resultItem).offset(-12);
            make.top.equalTo(resultItem).offset(12);
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(20);
        }];
        
        [conflictLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(resultItem).offset(12);
            make.bottom.equalTo(resultItem).offset(-12);
        }];
        
        [detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(resultItem).offset(-12);
            make.bottom.equalTo(resultItem).offset(-12);
        }];
    }
    
    [resultTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(resultCard).offset(16);
    }];
    
    // 设置约束
    [eventCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView).inset(16);
    }];
    
    [queryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(eventCard.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(48);
    }];
    
    [resultCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(queryButton.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(288);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
}

- (void)setupConstraints {
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(60);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView).offset(16);
        make.centerY.equalTo(self.headerView);
        make.width.height.mas_equalTo(44);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.headerView);
    }];
}

#pragma mark - Actions

- (void)backButtonTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)eventTapped:(UITapGestureRecognizer *)tap {
    NSInteger index = tap.view.tag;
    NSLog(@"选择了事件: %ld", (long)index);
    // TODO: 处理事件选择
}

- (void)queryButtonTapped {
    NSLog(@"查询吉日");
    // TODO: 实现查询逻辑
}

@end 
